package jason.test.dirtest;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.widget.TextView;


public class MainActivity extends Activity {
    SensorManager sm ;
    DirSensor sd;
    Sensor accsensor;
    Sensor magsensor;
    TextView tv ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView)findViewById(R.id.textview);
        sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sd = new DirSensor();
        sd.setHandler(new Handler(new Handler.Callback() {
			
			@Override
			public boolean handleMessage(Message msg) {
				tv.setText(sd.getDataDescription()
						+ "\n\n"
						+ "Angle OF View:"+sd.getAngleOfView());
				return true;
			}
		}));
        
//        List<Sensor> ss = sm.getSensorList(Sensor.TYPE_ALL);
//        String slist = "";
//        for(Sensor s:ss)
//        	slist += s.getType() + ","+s.getName() + "\n";
//        tv.setText(slist);
//        Log.v("GET SENSOR", slist);
        accsensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        //Log.v("GET SENSOR", accsensor.getName());
        magsensor = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        sm.registerListener(sd, magsensor, SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(sd, accsensor, SensorManager.SENSOR_DELAY_NORMAL);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);        
        return true;
    }
    
    @Override
    protected void onPause() { 
    	sm.unregisterListener(sd);
    	
    	super.onPause();
    }
    
    @Override
    protected void onStart() {
    	sm.registerListener(sd, accsensor, SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(sd, magsensor, SensorManager.SENSOR_DELAY_NORMAL);
        
    	super.onStart();
    }
    
}
